package org.apache.oozie.tools.workflowgenerator.client.property.control;

import java.util.ArrayList;
import java.util.List;

import org.apache.oozie.tools.workflowgenerator.client.property.Property;
import org.apache.oozie.tools.workflowgenerator.client.property.PropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.xml.client.Text;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class WrkflowPropertyTable extends PropertyTable {

    protected Grid grid;
    protected TextBox jt;
    protected TextBox nn;
    protected List<Property> configs;
    protected ListBox namespace;

    public WrkflowPropertyTable(NodeWidget w) {
        super(w);
        initConf();
        initWidget();
    }

    /**
     * Initialize configuration
     */
    protected void initConf() {
        configs = new ArrayList<Property>();
        configs.add(new Property("  ", "  "));
    }

    /**
     * Create a label with common format
     */
    @Override
    protected Label createLabel(String name) {
        Label label = new Label(name);
        label.setWidth("200px");
        label.setHeight("7px");
        label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        return label;
    }

    /**
     * Generate xml elements of workflow (such as global section) and attach them
     * to xml doc
     */
    public void generateXML(Document doc, Element root, NodeWidget next) {

        if (namespace != null && namespace.getSelectedIndex() < 3)
            return;

        // create <global>
        Element global = doc.createElement("global");

        // create <job-tracker>
        if (jt.getText() != null && !jt.getText().matches("\\s*")) {
            Element jtele = doc.createElement("job-tracker");
            Text t = doc.createTextNode(jt.getText());
            jtele.appendChild(t);
            global.appendChild(jtele);
        }

        // create <name-node>
        if (nn.getText() != null && !nn.getText().matches("\\s*")) {
            Element nnele = doc.createElement("name-node");
            Text n = doc.createTextNode(nn.getText());
            nnele.appendChild(n);
            global.appendChild(nnele);
        }

        // create <configuration>
        configToXML(configs, global, doc);

        // add <global> only when child nodes exist(if not, do not put <global>)
        if (global.hasChildNodes())
            root.appendChild(global);
    }

    /**
     * Initialize widgets shown in propery table
     */
    protected void initWidget() {

        grid = new Grid(5, 2);
        this.add(grid);

        this.setAlwaysShowScrollBars(true);
        this.setSize("100%", "80%");

        // insert row for Name
        grid.setWidget(0, 0, createLabel("Name"));
        name = new TextBox();
        name.setText("WrkflowGeneratorDemo"); // default value
        grid.setWidget(0, 1, formatCell(name));

        // insert row for Namespace
        grid.setWidget(1, 0, createLabel("NameSpace"));
        namespace = new ListBox();
        grid.setWidget(1, 1, createNameSpaceList(namespace));

        // insert row for JobTracker
        grid.setWidget(2, 0, createLabel("Global JobTracker"));
        jt = new TextBox();
        grid.setWidget(2, 1, formatCell(jt));

        // insert row for NameNode
        grid.setWidget(3, 0, createLabel("Global NameNode"));
        nn = new TextBox();
        grid.setWidget(3, 1, formatCell(nn));

        // insert row for Global Configuration
        grid.setWidget(4, 0, createLabel("Global Configuration"));
        grid.setWidget(4, 1, createSubTable("Property Name", "Value", configs, null));

    }

    /**
     * Create a drop down list of name spaces
     *
     * @param b list box
     * @return
     */
    protected ListBox createNameSpaceList(ListBox b) {
        if (b == null)
            b = new ListBox();
        b.addItem("uri:oozie:workflow:0.1");
        b.addItem("uri:oozie:workflow:0.2");
        b.addItem("uri:oozie:workflow:0.3");
        b.addItem("uri:oozie:workflow:0.4");
        b.setSelectedIndex(2);

        return b;
    }

    /**
     * Return a name space
     *
     * @return
     */
    public String getNameSpace() {
        return namespace.getItemText(namespace.getSelectedIndex());
    }

}
